package cn.fl.product.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.bizflow.service.ISysBizFlowRlService;
import cn.fl.bizflow.vo.SysBizFlowRlVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.customer.vo.CstValidCardInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.service.*;
import cn.fl.product.transform.SplBscInfoApplyVOTransform;
import cn.fl.product.vo.*;
import cn.fl.system.vo.SysUlFilesRecVO;
import fl.constant.facade.framework.constant.DictionaryConstant;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.constant.FlowConstant;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.spl.facade.bizflow.condition.SysBizFlowRlQC;
import fl.spl.facade.spl.api.ISplInfoApplyAddApiService;
import fl.spl.facade.spl.api.ISplInfoApplyApiService;
import fl.spl.facade.spl.api.ISplInfoApplyModifyApiService;
import fl.spl.facade.spl.condition.SplBiApplyFlowRlQC;
import fl.spl.facade.spl.condition.SplRlPsnQueryCondition;
import fl.spl.facade.spl.constant.SplBscInfoApplyConstant;
import fl.spl.facade.spl.constant.SplInfoConstant;
import fl.spl.facade.spl.dto.SplBiApplyFlowRlDTO;
import fl.spl.facade.spl.dto.SplBscInfoApplyDTO;
import fl.spl.facade.spl.dto.SplInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SplBscInfoApplyServiceImpl implements ISplBscInfoApplyService {

    @Autowired
    private ISplInfoApplyApiService splInfoApplyApiService;

    @Autowired
    private ISplInfoApplyAddApiService splInfoApplyAddApiService;

    @Autowired
    private ISplInfoApplyModifyApiService splInfoApplyModifyApiService;

    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    @Autowired
    private ISplInfoService prdSplInfoService;

    @Autowired
    private ISplRlPsnService splRlPsnService;

    @Autowired
    private ICstBscInfoService cstBscInfoService;

    @Autowired
    private ISysBizFlowRlService sysBizFlowRlService;

    @Autowired
    private IFlowQueryApiService flowQueryApiService;

    @Autowired
    private ISplBiApplyRlPsnService splBiApplyRlPsnService;

    @Autowired
    private ISplBiApplyFlowService splBiApplyFlowService;

    /**
     * 组织架构API暴露服务
     */
    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Override
    public SplBscInfoApplyVO getSplBscInfoApplyById(Long id, Long splId) {
        return SplBscInfoApplyVOTransform.toVO(this.splInfoApplyApiService.getBscInfoApplyById(id, splId));
    }

    @Override
    public boolean saveBscInfoApply(SplBscInfoApplyVO vo) throws BaseException {
        if (StringUtils.isNotEmpty(vo.getId())) {
            return splInfoApplyModifyApiService.modifySplBscInfoApply(SplBscInfoApplyVOTransform.toDTO(vo));
        } else {
            Long rtn = splInfoApplyAddApiService.addSplBscInfoApply(SplBscInfoApplyVOTransform.toDTO(vo));
            if (rtn > 0) {
                vo.setId(rtn);
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 启动供应商变更工作流程
     *
     * @param flowStartParamsDTO
     * @return
     * @throws BaseException
     */
    @Override
    public DataResultDTO addSplApplyFlowStart(FlowStartParamsDTO flowStartParamsDTO) throws BaseException {
        return this.splInfoApplyApiService.addSplApplyFlowStart(flowStartParamsDTO);
    }

    @Override
    public Pager searchSplApplyListPage(Pager page) throws BaseException {
        page = this.splInfoApplyApiService.searchSplApplyListPage(page,
                CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
        List<SplBscInfoApplyDTO> splBscInfoApplyDTOList = (List<SplBscInfoApplyDTO>) page.getResultList();
        List<SplBscInfoApplyVO> splBscInfoApplyVOList = SplBscInfoApplyVOTransform.toVOList(splBscInfoApplyDTOList);
        page.setResultList(splBscInfoApplyVOList);
        return page;
    }

    @Override
    public List<KeyValueVO> getKvList() throws BaseException {
        List<KeyValueVO> kvList = new ArrayList<>();
        List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(),
                CurrentThreadContext.getCurrentOrgCd(),
                PropertiesCacheUtils.getProperty(DictionaryConstant.FLW_ZK_FK_CD));
        if (CollectionUtils.isNotEmpty(sysDictionaryDTOList)) {
            for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
                KeyValueVO keyValueVO = new KeyValueVO();
                keyValueVO.setKey(sysDictionaryDTO.getCode());
                keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
                kvList.add(keyValueVO);
            }
        }
        return kvList;
    }

    /**
     * 检查供应商是否存在审核中的变更流程
     *
     * @param splId
     * @param sysCd
     * @param orgCd
     * @return true 存在 false 不存在
     * @throws BaseException
     */
    @Override
    public boolean checkFlowStatus(Long splId, String sysCd, String orgCd) throws BaseException {
        return this.splInfoApplyApiService.checkFlowStatus(splId, sysCd, orgCd);
    }

    /**
     * 判断供应商主要信息是否发生变化
     * @param entity
     * @param sysCd
     * @param orgCd
     * @throws BaseException
     */
    @Override
    public void checkBscInfo(SplInfoDTO entity, String sysCd, String orgCd) throws BaseException{
        this.splInfoApplyApiService.checkBscInfo(entity,sysCd,orgCd);
    }

    /**
     * 查询厂商下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchMfrKVList()throws BaseException{
        return  this.prdSplInfoService.searchMfrKVList();
    }
    /**
     * 查询资本单位下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchCapUntCdList()throws BaseException{
        return this.prdSplInfoService.searchCapUntCdList();
    }

    /**
     * 查询经营品牌下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchBrandList(String code)throws BaseException{
        return this.prdSplInfoService.searchBrandList(code);
    }

    /**
     * 查询企业性质下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchCorpTypCdList()throws BaseException{
        return this.prdSplInfoService.searchCorpTypCdList();
    }

    /**
     * 查询事业部下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchBuOrgCdList()throws BaseException{
        return this.prdSplInfoService.searchBuOrgCdList();
    }

    /**
     * 查询项目类型
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchPrjTypCdList()throws BaseException{
        return this.prdSplInfoService.searchPrjTypCdList();
    }

    /**
     * 查询供应商级别下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchSplLvlCdList()throws BaseException{
        return this.prdSplInfoService.searchSplLvlCdList();
    }

    /**
     * 查询立项级别下拉框数据
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchPrjLvlCdList()throws BaseException{
        return this.prdSplInfoService.searchPrjLvlCdList();
    }

    /**
     * 获取唯一对象
     * @param id 主键
     * @return 对象DTO
     */
    @Override
    public SplInfoVO selectSplInfoVOById(Long id)throws BaseException{
        return this.prdSplInfoService.selectById(id);
    }

    /**
     * 联系人证件类型<KEY,VALUE>集合
     * @return <KEY,VALUE>集合
     */
    @Override
    public List<KeyValueVO> searchCertTypCdList()throws BaseException{
        return this.splRlPsnService.searchCertTypCdList();
    }

    /**
     * 联系人类型
     * @return
     * @throws BaseException
     */
    @Override
    public List<KeyValueVO> searchRlTypCdList()throws BaseException{
        return this.splRlPsnService.searchRlTypCdList();
    }

    /**
     * 分页查询所有
     * @param page
     * @return
     */
    @Override
    public Pager searchSplRlPsnListPage(Pager page)throws BaseException{
        return this.splRlPsnService.searchListPage(page);
    }

    /**
     * 不分页查询所有
     * @param qc
     * @return
     * @throws BaseException
     */
    @Override
    public List<SysBizFlowRlVO> searchSysBizFlowRlList(SysBizFlowRlQC qc)throws BaseException{
        return this.sysBizFlowRlService.searchList(qc);
    }

    /**
     * 验证客户身份证号码和名称是否有法律效应
     * @param certNm 客户姓名
     * @param certNo 客户身份证号
     * @return
     * @throws Exception
     */
    @Override
    public CstValidCardInfoVO validateCstCert(String certNm, String certNo)throws BaseException{
        return  this.cstBscInfoService.validateCstCert(certNm, certNo);
    }

    /**
     * 修改
     * @param entity
     * @return
     */
    @Override
    public boolean modifySplInfoVO(SplInfoVO entity)throws BaseException{
        return this.prdSplInfoService.modify(entity);
    }

    /**
     * 根据条件查询数据,其中系统代码、业务主键、动态表名中间字不能为空.
     * @param condition 查询条件对象
     * @return 集合
     * @throws BaseException
     */
    @Override
    public List<SysFlowSHDTO> searchFlowSHList(SysFlowSHQueryCondition condition) throws BaseException{
        return this.flowQueryApiService.searchFlowSHList(condition);
    }

    /**
     * 插入流程中的数据
     * @param list
     * @return
     * @throws BaseException
     */
    @Override
    public boolean batchAddForFlow(List<SysBizFlowRlVO> list)throws BaseException{
        return this.sysBizFlowRlService.batchAddForFlow(list);
    }

    /**
     * 批量新增
     * @param list
     * @return
     * @throws BaseException
     */
    @Override
    public boolean batchAdd(List<SysBizFlowRlVO> list)throws BaseException{
        return this.sysBizFlowRlService.batchAdd(list);
    }

    /**
     * 供应商资料新增
     * @param fileList
     * @return
     * @throws BaseException
     */
    @Override
    public boolean batchAddSysUlFilesList(List<SysUlFilesRecVO> fileList)throws BaseException{
        return this.prdSplInfoService.batchAddFileList(fileList);
    }

    /**
     * 查询列表
     * @param qc
     * @return
     */
    @Override
    public List<SplRlPsnVO> searchSplRlPsnVOList(SplRlPsnQueryCondition qc){
        return this.splRlPsnService.searchList(qc);
    }

    /**
     * 通过主键信息查询供应商审核权限信息
     * @param applyId
     * @return
     */
    @Override
    public List<SplBiApplyFlowRlVO> getSplApplyFlow(Long applyId){
        return this.splBiApplyFlowService.getSplApplyFlow(applyId);
    }

    /**
     * 通过主键信息查询供应商主要联系人变更信息
     * @param id
     * @return
     */
    @Override
    public List<SplBiApplyRlPsnVO> getSplRlPsnApplyById(Long id, Long applyId){
        return  this.splBiApplyRlPsnService.getSplRlPsnApplyById(id,applyId);
    }

    /**
     * 保存主要联系人审核信息
     * @param vos
     * @throws BaseException
     */
    @Override
    public void saveBiApplyRlPsn(List<SplBiApplyRlPsnVO> vos,Long applyId) throws BaseException{
        this.splBiApplyRlPsnService.saveBiApplyRlPsn(vos,applyId);
    }

    /**
     * 保存供应商审核权限信息
     * @param vo
     * @return
     */
    @Override
    public boolean saveSplApplyFlow(SplBiApplyFlowRlVO vo){
        return this.splBiApplyFlowService.saveSplApplyFlow(vo);
    }

    /**
     * 修改主要联系人其他信息
     * @param vos
     * @throws BaseException
     */
    @Override
    public void modifySplBiPsn(List<SplRlPsnVO> vos) throws BaseException{
        this.splBiApplyRlPsnService.modifySplBiPsn(vos);
    }

    /**
     * 判断车辆个数是否大于10辆
     * @param applyId
     * @return
     */
    @Override
    public Integer getFlowNumcount(Long applyId){
        SplBiApplyFlowRlQC qc=new SplBiApplyFlowRlQC();
        qc.setSplApplyId(applyId);
        //获取变更个数
        List<SplBiApplyFlowRlDTO> list=this.splInfoApplyApiService.getSplFlowApplyList(qc);
        if(null != list && list.size() >0){
            SplBiApplyFlowRlDTO dto=list.get(0);
            Integer flowNum=dto.getFlowNum();
            if(null != flowNum){
                if(flowNum > SplBscInfoApplyConstant.SPL_FLOW_MAX_CAR_COUNT){
                    return 1;
                }
            }
        }
        return 0;
    }

    /**
     * 通过供应商id 判断该供应商是否存在变更中流程
     * @param splId
     * @return true 存在变更流程 false 不存在变更流程
     */
    @Override
    public boolean checkModifyStatus(Long splId) throws BaseException {
        boolean rtn = false;
        List<Long> params = new ArrayList<Long>();
        params.add(splId);
        List<Map> idMaps = this.splInfoApplyApiService.getSplBscInfoApplyBySplIds(params);
        if (null != idMaps && idMaps.size() > 0) {
            for (Map idMap : idMaps) {
                Long id=(Long)idMap.get("SPL_ID");
                if (splId.equals(id)) {
                    rtn = true;
                }
            }
        }
        return rtn;
    }

    /**
     * 通过供应商id 判断该供应商是否存在审核中的流程
     *
     * @param splId
     * @return
     */
    @Override
    public String getFlowMessage(Long splId) throws BaseException {
        String sysCd = CurrentThreadContext.getCurrentSysCd();
        //查询此供应商 在当前登录人所属事业部 是否有流程记录
        SysFlowSHQueryCondition condition = new SysFlowSHQueryCondition();
        condition.setSysCd(sysCd);
        condition.setDataId(splId);
        condition.setWordsInTheTable(SplInfoConstant.SPL_FLOW_S_H_SPL);
        condition.setBusTyp(FlowConstant.FLOW_MODEL_TYP_SPL_ACCESS);

        List<String> audStsCdList = new ArrayList<String>();
        audStsCdList.add(FlowConstant.FLOW_CHECK_STATE_IN);
        audStsCdList.add(FlowConstant.FLOW_CHECK_STATE_REJECT);
        condition.setAudStsCdList(audStsCdList);
        condition.setStatus(BaseConstant.IS_YESNO_YES);

        Map<String,String> searchBuOrgMap = this.searchBuOrgCdMap();

        List<SysFlowSHDTO> flowList = this.flowQueryApiService.searchFlowSHList(condition);
        if (null != flowList && flowList.size() > 0) {
            String message = "";
            for (SysFlowSHDTO dto : flowList) {
                String buOrgCd = dto.getBuOrgCd();
                if(StringUtils.isNotBlank(buOrgCd)){
                    String buOrgCdNm=searchBuOrgMap.get(buOrgCd);
                    if(StringUtils.isNotBlank(buOrgCdNm)){
                        message=message+buOrgCdNm+",";
                    }
                }
            }
            return message;
        }
        return null;
    }

    public Map<String,String> searchBuOrgCdMap() throws BaseException {
        List<SysOrgDTO> dtoList = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
        Map<String,String> map=new HashMap<>();
        if(CollectionUtils.isNotEmpty(dtoList)){
            for (SysOrgDTO dto : dtoList) {
                map.put(dto.getOrgCd(),dto.getOrgDispNm());
            }
        }
        return map;
    }

    @Override
    public List<KeyValueVO> searchNewCarTyp() throws BaseException {
        // TODO Auto-generated method stub
        return this.prdSplInfoService.searchNewCarTyp();
    }

    @Override
    public List<KeyValueVO> searchUsedCarTyp() throws BaseException {
        // TODO Auto-generated method stub
        return this.prdSplInfoService.searchUsedCarTyp();
    }

    @Override
    public List<KeyValueVO> searchBizScopeTyp() throws BaseException {
        // TODO Auto-generated method stub
        return this.prdSplInfoService.searchBizScopeTyp();
    }

    @Override
    public List<KeyValueVO> searchOwnerCdList() throws BaseException {
        // TODO Auto-generated method stub
        return this.prdSplInfoService.searchOwnerCdList();
    }

	@Override
	public Long addSplBscInfoApply(SplBscInfoApplyVO vo) throws BaseException {
		// TODO Auto-generated method stub
		return this.splInfoApplyAddApiService.addSplBscInfoApply(SplBscInfoApplyVOTransform.toDTO(vo));
	}

	@Override
	public boolean checkSplGSFlowStatus(Long splId) throws BaseException {
		// TODO Auto-generated method stub
		return this.splInfoApplyApiService.checkSplGSFlowStatus(splId, CurrentThreadContext.CURRENT_SYS_CD, CurrentThreadContext.CURRENT_ORG_CD);
	}
	
}
